package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import java.util.ArrayList;
import java.util.List;

import java.util.ArrayList;
import java.util.List;

import java.util.ArrayList;
import java.util.List;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;


    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
    /*
    互相喜欢，喜欢，粉丝 - 统计
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){

        Map likeVoMap = new HashMap();
        Map counts=userInfoService.counts(likeVoMap);

        return ResponseEntity.ok(counts);
    }

    /*
    互相喜欢、喜欢、粉丝 分页
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") String type,
                                  @RequestParam(defaultValue = "1")int page,
                                  @RequestParam(defaultValue = "10")int pageSize,
                                  String nickName

    ){
        PageResult pageResult = userInfoService.findOther(type,page,pageSize,nickName);

        return ResponseEntity.ok(pageResult);
    }
     /*
     是否喜欢
      */
    @GetMapping("{uid}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable("uid") Long likeUserId){


        Boolean like=userInfoService.alreadyLove(UserHolder.getUserId(),likeUserId);

        return ResponseEntity.ok(like);
    }
    /*
    粉丝喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable("uid") Long fansUserId){


        userInfoService.fansLike(fansUserId);

        return ResponseEntity.ok(null);

    }

    /*
    取消喜欢
     */
    @DeleteMapping("like/{uid}")
    public ResponseEntity notLike(@PathVariable("uid") Long likeUserId){
        userInfoService.notLike(likeUserId);

        return ResponseEntity.ok(null);
    }


    /**
     * 头像修改
     *
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        userInfoService.updateHead(headPhoto, UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }

    /**
     * 发送短信验证码
     *
     * @return
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //获得当前手机号
        String mobile = UserHolder.getMobile();
        //调用service
        userService.sendVerificationCode(mobile);
        return ResponseEntity.ok(null);

    }

    /**
     * 校验验证码
     *
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> map) {
        //获得验证码
        String verificationCode = (String) map.get("verificationCode");
        //调用service
        boolean b = userService.checkVerificationCode(verificationCode);
        //封装
        Map<String, Boolean> vo = new HashMap<>();
        vo.put("verification", b);
        return ResponseEntity.ok(vo);
    }


    /**
     * 保存
     *
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map<String, String> map) {
        //获得手机号
        String phone = map.get("phone");
        //调用service
        userService.phone(phone);
        return ResponseEntity.ok(null);
    }
}
